package com.ucode.application.controller.sys;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ucode.sys.service.DeptService;
import com.ucode.sys.service.UserService;
import com.ucode.sys.vo.DeptTreeNode;
import com.ucode.sys.vo.UserVo;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="系统公共接口")
@RestController
@RequestMapping("/sys-common")
public class SysCommonController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private DeptService deptService;
    
    @ApiOperation("分页查询用户列表")
    @GetMapping(value = "/user/list")
    public ResponseResult<Object> findPage(
            @ApiParam(value ="姓名")  @RequestParam(value = "realname",required=false) String realname,
            @ApiParam(value ="登录名")  @RequestParam(value = "username",required=false) String username,
            @ApiParam(value ="手机号")  @RequestParam(value = "mobile",required=false) String mobile,
            @ApiParam(value ="部门")  @RequestParam(value = "deptId",required=false) Long deptId,
            @ApiParam(value ="状态")  @RequestParam(value = "statuz",required=false) Integer statuz,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        
        if(StrUtil.isNotBlank(realname))
            realname = StrUtil.concat(true,realname, "%");
        if(StrUtil.isNotBlank(username))
            username = StrUtil.concat(true,username, "%");
        if(StrUtil.isNotBlank(mobile))
            mobile = StrUtil.concat(true,mobile, "%");
        
        Paginator<UserVo> page = userService.queryPage(realname, username, mobile, deptId, statuz, pageNum, pageSize);
        
        List<UserVo> userList = page.getList();
        if(CollUtil.isNotEmpty(userList)){
            List<Long> userIds = userList.stream().map(m -> m.getId()).distinct().collect(Collectors.toList());
            
        }
        
        return ResponseResult.success(page);
    }
    
    @ApiOperation("获取部门树形结构")
    @GetMapping(value = "/dept/tree")
    public ResponseResult<Object> tree(){
        List<DeptTreeNode> tree = deptService.findTree();
        return ResponseResult.success(tree);
    }
    
}
